/**
 * <pre>
 * Title: 		ComplexMinutelyIterator.java
 * Project: 	HP-Common
 * Author:		linriqing
 * Create:	 	2009-6-12 下午05:02:28
 * Copyright: 	Copyright (c) 2009
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.common.scheduling.iterators;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.tiling.scheduling.ScheduleIterator;

/**
 * <pre>
 * 功能更强大的MinutelyIterator, 支持每天在固定时间点开始执行, 固定时间点停止
 * </pre>
 * @author linriqing
 * @version 1.0, 2009-6-12
 */
public class ComplexMinutelyIterator implements ScheduleIterator
{
	private int interval;

	private long timeinMillonSeconds;

	private static final Log logger = LogFactory.getLog(ComplexMinutelyIterator.class);

	private final Calendar startTime;

	private final Calendar endTime;

	/**
	 * 构造函数
	 * @param interval 迭代的分钟数
	 */
	public ComplexMinutelyIterator(int interval, String[] timeStrings)
	{
		this.interval = interval;
		this.timeinMillonSeconds = new Date().getTime();

		List<Date> dateList = new ArrayList<Date>();

		// 主要是兼容类似10:7:00方式的时间参数
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		for (int i = 0; i < timeStrings.length; i++)
		{
			try
			{
				String timeStr = timeStrings[i];
				Date date = df.parse(timeStr);
				if (!dateList.contains(date))
				{
					dateList.add(date);
				}
			}
			catch (ParseException e)
			{
				logger.error("时间格式错误", e);
				continue;
			}
		}

		Collections.sort(dateList);
		startTime = Calendar.getInstance();
		endTime = Calendar.getInstance();
		Calendar calendar = Calendar.getInstance();
		if (dateList.size() > 0)
		{
			startTime.setTime(dateList.get(0));
			startTime.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
		}
		else
		{
			startTime.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0,
					0);

		}
		if (dateList.size() > 1)
		{
			endTime.setTime(dateList.get(1));
			endTime.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
		}
		else
		{
			endTime.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 24, 0,
					0);
		}

		timeinMillonSeconds = (startTime.getTimeInMillis() - (interval * 60 * 1000));
	}

	public Date next()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(timeinMillonSeconds);
		do
		{
			calendar.add(Calendar.MINUTE, interval);
		}
		while (calendar.getTimeInMillis() < System.currentTimeMillis());

		if (calendar.before(startTime))
		{
			calendar.setTimeInMillis(startTime.getTimeInMillis());
		}

		if (calendar.after(endTime))
		{
			startTime.add(Calendar.DATE, 1);
			endTime.add(Calendar.DATE, 1);
			calendar.setTimeInMillis(startTime.getTimeInMillis());
		}
		this.timeinMillonSeconds = calendar.getTimeInMillis();
		return calendar.getTime();
	}
}
